<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-for</title>
</head>

<body>
    <div id="app">
        <h2>望霸霸书架</h2>
        <ul>
            <!--key属性="唯一标识"-->
            <!--作用：给列表添加唯一标识。便于Vue进行列表的正确排序复用-->
            <li v-for="(item) in bookList" :key="item.id">
               <span>{{item.name}}</span>
               <span>{{item.author}}</span>
               <button @click="del(item.id)">删除</button>
            </li>
        </ul>
    </div>

    <!-- 引入包-->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                index: 0,
                bookList: [
                    {id:1,name:'《红楼梦》',author:'曹雪芹'},
                    {id:2,name:'《西游记》',author:'吴承恩'},
                    {id:3,name:'《水浒传》',author:'施耐庵'},
                    {id:4,name:'《三国演义》',author:'罗贯中'}
                ]
            },
            methods:{
                del(id){
                    console.log('del'+id);
                    //通过id进行删除数组中的对应项 -> filter(不会改变原数组)
                    //filter:根据条件，保留满足条件的对应项，得到一个新数组
                    this.bookList=this.bookList.filter(item=>item.id !== id);
                }
            }
        })
    </script>
</body>

</html>